#!/bin/bash
# 用法：
#   ./build.sh BuildType=test NotifyType=test BuildAnd=true BuildDesc=file:../thisBuildDesc.md EmailTo=tel8617706713271@163.com
#   ./build.sh BuildType=test NotifyType=test BuildDesc=file:../thisBuildDesc.md EmailTo=tel8617706713271@163.com
#   ./build.sh BuildType=pub NotifyType=test BuildAnd=true BuildDesc=file:../thisBuildDesc.md EmailTo=tel8617706713271@163.com Upload2GooglePlay=true
#   ./build.sh EnvDev=true EnvTest=true EnvPre=true EnvPub=true p1=2 p2=2 p3=2 p4=2 p5=2 p6=2 p7=2 p8=2 p9=2

# export PATH=${PATH}:/usr/local/bin/

sigterm_handler() {
  echo "Shutdown signal received."
  exit 1
}

## Setup signal trap
trap 'trap " " SIGINT SIGTERM SIGHUP; kill 0; wait; sigterm_handler' SIGINT SIGTERM SIGHUP

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

export SCRIPT_DIR=$(
  cd $(dirname ${BASH_SOURCE[0]})
  pwd
)
cd $SCRIPT_DIR
pwd
source $SCRIPT_DIR/build_proxy_cfg.sh
source $SCRIPT_DIR/shlog.sh

buildLog scriptDir:$SCRIPT_DIR

proxy_on
which java
java --version
which fastlane
fastlane --version
which python3
python3 --version
which git
git --version
which ruby
ruby -v
proxy_off

export BuildUpload2Pgy="true"
export BuildNotifyFs="true"
export BuildNotifyEmail="true"
export BuildType=""
export NotifyType=""
export BuildAnd=""
export BuildDesc=""
export BuildBy=""
export EmailTo=""
export Upload2GooglePlay=""
export EnvDev="true"
export EnvTest="true"
export EnvPre="true"
export EnvPub="true"

paramCount=$#
while [ $paramCount -ge 0 ]; do # this is loop1
  param=${!paramCount}
  buildLog $paramCount:$param
  if [[ $param == *"="* ]]; then
    arrIN=(${param//=/ })
    # buildLog ${arrIN[0]}:${arrIN[1]}
    if [[ ${arrIN[0]} == "BuildType" ]]; then
      BuildType=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "NotifyType" ]]; then
      NotifyType=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "BuildAnd" ]]; then
      BuildAnd=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "BuildBy" ]]; then
      BuildBy=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "EmailTo" ]]; then
      EmailTo=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "Upload2GooglePlay" ]]; then
      Upload2GooglePlay=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "BuildDesc" ]]; then
      BuildDesc=${arrIN[1]}
      if [[ $BuildDesc == "file:"* ]]; then
        BuildDesc=${BuildDesc/"file:"/}
        # 不在需要相对路径 BuildDesc="${SCRIPT_DIR}/${BuildDesc}"
      fi
    fi
    if [[ ${arrIN[0]} == "EnvDev" ]]; then
      EnvDev=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "EnvTest" ]]; then
      EnvTest=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "EnvPre" ]]; then
      EnvPre=${arrIN[1]}
    fi
    if [[ ${arrIN[0]} == "EnvPub" ]]; then
      EnvPub=${arrIN[1]}
    fi
  fi
  paramCount=$(expr $paramCount - 1)
done

buildParams="
BuildType:$BuildType
NotifyType:$NotifyType
BuildAnd:$BuildAnd
BuildDesc:$BuildDesc
BuildBy:$BuildBy
EmailTo:$EmailTo
Upload2GooglePlay:$Upload2GooglePlay
EnvCfg:EnvDev:$EnvDev,EnvTest:$EnvTest,EnvPre:$EnvPre,EnvPub:$EnvPub
"
buildLog "------------------->$buildParams <--------------"

if [ $JENKINS_HOME ]; then
  buildLog "JENKINS_HOME = $JENKINS_HOME"
  export JenkinsBuildDescFile="${SCRIPT_DIR}/$(date '+%Y-%m-%d_%H-%M-%S')_jenkinsBuildDesc.tmp"
  rm -rf $JenkinsBuildDescFile
  echo "jenkinsHome: $JENKINS_HOME" >"$JenkinsBuildDescFile"
  echo "branchName: $(git branch --show-current)" >>"$JenkinsBuildDescFile"
  echo "buildTime: $(date '+%Y-%m-%d %H:%M:%S')" >>"$JenkinsBuildDescFile"
  echo "buildDisplayName: $BUILD_DISPLAY_NAME" >>"$JenkinsBuildDescFile"
  echo "buildUrl: $BUILD_URL" >>"$JenkinsBuildDescFile"
  echo "jobBaseName: $JOB_BASE_NAME" >>"$JenkinsBuildDescFile"
  echo "build by $BUILD_USER($BUILD_USER_EMAIL)" >>"$JenkinsBuildDescFile"
  echo "buildParams:$buildParams" >>"$JenkinsBuildDescFile"
else
  buildLog "JENKINS_HOME IS NOT EXISTS"
  cd $SCRIPT_DIR
  cd ..
  export JENKINS_HOME=$(pwd)
  export BUILD_TAG='local'
  buildLog "jenkinsHome: $JENKINS_HOME"
  export JenkinsBuildDescFile="${SCRIPT_DIR}/$(date '+%Y-%m-%d_%H-%M-%S')_jenkinsBuildDesc.tmp"
  rm -rf $JenkinsBuildDescFile
  echo "projectDir: $JENKINS_HOME" >"$JenkinsBuildDescFile"
  echo "branchName: $(git branch --show-current)" >>"$JenkinsBuildDescFile"
  echo "buildTime: $(date '+%Y-%m-%d %H:%M:%S')" >>"$JenkinsBuildDescFile"
  echo "build by $USER" >>"$JenkinsBuildDescFile"
  echo "buildParams:$buildParams" >>"$JenkinsBuildDescFile"
fi

EnvCfg="\"\""
if [ "$EnvDev" == "true" ]; then
  EnvCfg="\"dev\",$EnvCfg"
fi
if [ "$EnvTest" == "true" ]; then
  EnvCfg="\"test\",$EnvCfg"
fi
if [ "$EnvPre" == "true" ]; then
  EnvCfg="\"pre\",$EnvCfg"
fi
if [ "$EnvPub" == "true" ]; then
  EnvCfg="\"pub\",$EnvCfg"
fi
echo "--------->写入环境配置-------------"

echo "[$EnvCfg]" > $SCRIPT_DIR/../sk-basic-wrap/src/main/assets/net_cfg/nef_cfg.json
echo "开始执行--------->artifact_build-------------"
echo "开始执行--------->artifact_build-------------"
echo "开始执行--------->artifact_build-------------"


sh $SCRIPT_DIR/artifact_build.sh
rm -rf $JenkinsBuildDescFile